
from data_backtest.index_backtest_thresholds import *
from data_backtest.index_backtest_moving_average import *

# 因子方向转换
def indicator_convert(df_basic, flag=1):
    '''
    :param df_basic: 因子原始数据
    :param flag: 因子分类，1为正向，0为负向
    :return: dataframe数据
    '''

    if flag == 0:
        df_basic.iloc[:, -1] = - df_basic.iloc[:, -1]

    return df_basic

# 因子批量回测
def massive_backtest(df_basic, flag, filename):
    '''
    :param df_basic: 因子原始数据
    :param flag: 因子分类，1为正向，0为反向
    :param filename: 文件名
    :return: dataframe数据
    '''
    print('正在回测_{}'.format(filename))
    
    df_basic = indicator_convert(df_basic, flag)

    wb = pd.ExcelWriter('因子回测结果\\' + filename + '.xlsx')

    df_Roll = backtest(df_basic, 21, 21, 0.9, 0.2, 2)
    df_Roll.to_excel(wb, sheet_name='Roll', index=False)

    df_MACD = MACD(df_basic = df_basic)
    index_backtest(df_MACD).to_excel(wb, sheet_name='MACD', index=False)

    df_SMA = SMA(df_basic = df_basic)
    index_backtest(df_SMA).to_excel(wb, sheet_name='SMA', index=False)

    df_eight_equal_length = eight_equal_length(df_basic = df_basic)
    index_backtest(df_eight_equal_length).to_excel(wb, sheet_name='八均线', index=False)

    df_bull_arrange = bull_arrange(df_basic = df_basic)
    index_backtest(df_bull_arrange).to_excel(wb, sheet_name='多头排列', index=False)

    wb.close()